package com.ddz.qpz.controller.admin;

import com.ddz.common.exception.BusinessException;
import com.ddz.common.result.BaseResult;
import com.ddz.common.result.ResponseEnum;
import com.ddz.qpz.service.CityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;

/**
 * @Author: LiQingBo
 * @Date: 2021/11/7-11-07-11:57
 * @Description: com.ddz.qpz.controller.admin
 * @version: 1.0
 */
@Api(tags = "省市级联数据字典管理")
@RestController
@RequestMapping("/admin/core/city")
@Slf4j
public class AdminCityController {
    @Resource
    private CityService cityService;

    @ApiOperation("Excel数据的批量导入")
    @PostMapping("/import")
    public BaseResult batchImport(
            @ApiParam(value = "Excel数据字典文件", required = true)
            @RequestParam("file") MultipartFile file) {

        try {
            InputStream inputStream = file.getInputStream();
            cityService.importData(inputStream);

            return BaseResult.ok().message("数据字典数据批量导入成功");

        } catch (Exception e) {
            throw new BusinessException(ResponseEnum.UPLOAD_ERROR, e);
        }
    }
}
